// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html


import Card_Alpha from "../../common/script/Card_Alpha";
import PlayerInfo from "../../common/script/PlayerInfo";
import EgyptianAdventure from "./EgyptianAdventure";
import ns from "./EgyptianAdventureNS";




const {ccclass, property} = cc._decorator;


@ccclass
export default class CardCtrl extends cc.Component {

    game: EgyptianAdventure = null;

    reward: ns.Reward = {type: ns.RewardType.coin, value: 0};

    index: number = 0;
    cardAlpha: Card_Alpha = null;
    onLoad () {
        this.cardAlpha = this.node.getComponent(Card_Alpha);
        this.node.once('touchend', this.onClick, this);
    }

    init(index: number, reward: ns.Reward,game: EgyptianAdventure){
        this.index = index;
        this.reward = reward;
        this.game = game;
    } 

    



    start () {

    }

    onClick(){

        if(this.reward.type == ns.RewardType.mummy){
                    
            // this.game.cardRoot.children.forEach((item)=>{
            //     let com = item.getComponent(CardCtrl)
            //     item.off('touchend', com.onClick, this);
            // })
            this.game.touchMask.active = true;
        }

        this.cardAlpha.flipCard(0.5,cc.v3(0, -180, 0),false,()=>{

            this.scheduleOnce(()=>{
                if(this.reward.type == ns.RewardType.coin){
                    console.log(this.reward.value,"赢得金币")
                    PlayerInfo.ins.addMoney(Number(this.reward.value));
                    this.game.totalWin += Number(this.reward.value);
                }
                
                if(this.reward.type == ns.RewardType.mummy){
                    this.game.showGameOverPanel();
                }
                
                this.game.restCardCount --;
                this.game.checkGameOver();
            },0.6)
        });
    }

    // update (dt) {}
}
